//
//  ViewController.m
//  纯代码方式实现点击事件
//
//  Created by hyl on 16/4/24.
//  Copyright © 2016年 hyl. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
-(id)addWithView:(id (^)())view{
    return view();
}
-(void)viewDidLoad{
    [super viewDidLoad];
    //   当加载完后添加一些按键
    //    加号按键
    
//    [self.view addSubview:addButton];
    [self.view addSubview:[self addWithView:^id{
        UIButton *addButton=[[UIButton alloc]init];
        addButton.tag=10;//
        [addButton setBackgroundImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
        addButton.frame=CGRectMake(50, 40, 50, 50);
        [addButton setBackgroundImage:[UIImage imageNamed:@"add_highlighted"] forState:UIControlStateHighlighted];
        [addButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];//易导致循环引用
        return addButton;
    }]];
    
    
    //    删除
    UIButton *minusButton=[[UIButton alloc]init];
    [minusButton setBackgroundImage:[UIImage imageNamed:@"remove"] forState:UIControlStateNormal];
    [minusButton setBackgroundImage:[UIImage imageNamed:@"remove_highlighted"] forState:UIControlStateHighlighted];
    minusButton.tag=20;
    minusButton.frame=CGRectMake(270, 40, 50, 50);
    [minusButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:minusButton];
    //  添置两个按键有很多重复的代码可抽出
}
-(void)click:(UIButton *)button{
    if (button.tag==10) {
        NSLog(@"this is add");
    } else {
        NSLog(@"this is minus");
    }
}


@end
